home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / mus / misc / mpegaudiofix.lha / common.h < prev    next >
C/C++ Source or Header  |  1995-03-30  |  18KB  |  484 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. common.h
  4. **********************************************************************/
  5. /**********************************************************************
  6.  * MPEG/audio coding/decoding software, work in progress              *
  7.  *   NOT for public distribution until verified and approved by the   *
  8.  *   MPEG/audio committee.  For further information, please contact   *
  9.  *   Davis Pan, 508-493-2241, e-mail: pan@gauss.enet.dec.com          *
  10.  *                                                                    *
  11.  * VERSION 4.0                                                        *
  12.  *   changes made since last update:                                  *
  13.  *   date   programmers         comment                               *
  14.  * 2/25/91  Doulas Wong,        start of version 1.0 records          *
  15.  *          Davis Pan                                                 *
  16.  * 5/10/91  W. Joseph Carter    Reorganized & renamed all ".h" files  *
  17.  *                              into "common.h" and "encoder.h".      *
  18.  *                              Ported to Macintosh and Unix.         *
  19.  *                              Added additional type definitions for *
  20.  *                              AIFF, double/SANE and "bitstream.c".  *
  21.  *                              Added function prototypes for more    *
  22.  *                              rigorous type checking.               *
  23.  * 27jun91  dpwe (Aware)        Added "alloc_*" defs & prototypes     *
  24.  *                              Defined new struct 'frame_params'.    *
  25.  *                              Changed info.stereo to info.mode_ext  *
  26.  *                              #define constants for mode types      *
  27.  *                              Prototype arguments if PROTO_ARGS     *
  28.  * 5/28/91  Earle Jennings      added MS_DOS definition               *
  29.  *                              MsDos function prototype declarations *
  30.  * 7/10/91  Earle Jennings      added FLOAT definition as double      *
  31.  *10/ 3/91  Don H. Lee          implemented CRC-16 error protection   *
  32.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  33.  *                              important fixes involved changing     *
  34.  *                              16-bit ints to long or unsigned in    *
  35.  *                              bit alloc routines for quant of 65535 *
  36.  *                              and passing proper function args.     *
  37.  *                              Removed "Other Joint Stereo" option   *
  38.  *                              and made bitrate be total channel     *
  39.  *                              bitrate, irrespective of the mode.    *
  40.  *                              Fixed many small bugs & reorganized.  *
  41.  *                              Modified some function prototypes.    *
  42.  *                              Changed BUFFER_SIZE back to 4096.     *
  43.  * 7/27/92  Michael Li          (re-)Ported to MS-DOS                 *
  44.  * 7/27/92  Masahiro Iwadare    Ported to Convex                      *
  45.  * 8/07/92  mc@tv.tek.com                                             *
  46.  * 8/10/92  Amit Gulati         Ported to the AIX Platform (RS6000)   *
  47.  *                              AIFF string constants redefined       *
  48.  * 8/27/93 Seymour Shlien,      Fixes in Unix and MSDOS ports,        *
  49.  *         Daniel Lauzon, and                                         *
  50.  *         Bill Truerniet                                             *
  51.  **********************************************************************/
  52.  
  53. /***********************************************************************
  54. *
  55. *  Global Conditional Compile Switches
  56. *
  57. ***********************************************************************/
  58.  
  59. #define      UNIX            /* Unix conditional compile switch */
  60. /* #define      MACINTOSH       /* Macintosh conditional compile switch */
  61. /* #define      MS_DOS          /* IBM PC conditional compile switch */
  62. /* #define      MSC60           /* Compiled for MS_DOS with MSC v6.0 */
  63. /* #define      AIX             /* AIX conditional compile switch    */
  64. /* #define      CONVEX          /* CONVEX conditional compile switch */
  65.  
  66. #if defined(MSC60) 
  67. #ifndef MS_DOS
  68. #define MS_DOS
  69. #endif
  70. #ifndef PROTO_ARGS
  71. #define PROTO_ARGS
  72. #endif
  73. #endif
  74.  
  75. #ifdef  UNIX
  76. #define         TABLES_PATH     "tables"  /* to find data files */
  77. /* name of environment variable holding path of table files */
  78. #define         MPEGTABENV      "MPEGTABLES"
  79. #define         PATH_SEPARATOR  "/"        /* how to build paths */
  80. #endif  /* UNIX */
  81.  
  82. #ifdef  MACINTOSH
  83. /* #define      TABLES_PATH ":tables:"  /* where to find data files */
  84. #endif  /* MACINTOSH */
  85.  
  86. /* 
  87.  * Don't define FAR to far unless you're willing to clean up the 
  88.  * prototypes
  89.  */
  90. #define FAR /*far*/
  91.  
  92. #ifdef __STDC__
  93. #ifndef PROTO_ARGS
  94. #define PROTO_ARGS
  95. #endif
  96. #endif
  97.  
  98. #ifdef CONVEX
  99. #define SEEK_SET        0
  100. #define SEEK_CUR        1
  101. #define SEEK_END        2
  102. #endif
  103.  
  104. /* MS_DOS and VMS do not define TABLES_PATH, so OpenTableFile will default
  105.    to finding the data files in the default directory */
  106.  
  107. /***********************************************************************
  108. *
  109. *  Global Include Files
  110. *
  111. ***********************************************************************/
  112.  
  113. #include        <stdio.h>
  114. #include        <string.h>
  115. #include        <math.h>
  116.  
  117. #ifdef  UNIX
  118. #include        <unistd.h>
  119. #endif  /* UNIX */
  120.  
  121. #ifdef  MACINTOSH
  122. #include        <stdlib.h>
  123. #include        <console.h>
  124. #endif  /* MACINTOSH */
  125.  
  126. #ifdef  MS_DOS
  127. #include        <stdlib.h>
  128. #ifdef MSC60
  129. #include        <memory.h>
  130. #else
  131. #include        <alloc.h>
  132. #include        <mem.h>
  133. #endif  /* MSC60 */
  134. #endif  /* MS_DOS */
  135.  
  136. #ifdef AMIGA
  137. #include        <stdlib.h>
  138. #ifdef M68881
  139. #include <m68881.h>
  140. #endif
  141. #endif
  142.  
  143. /***********************************************************************
  144. *
  145. *  Global Definitions
  146. *
  147. ***********************************************************************/
  148.  
  149. /* General Definitions */
  150.  
  151. #ifdef  MS_DOS
  152. #define         FLOAT                   double
  153. #else
  154. #define         FLOAT                   float
  155. #endif
  156.  
  157. #define         FALSE                   0
  158. #define         TRUE                    1
  159. #define         NULL_CHAR               '\0'
  160.  
  161. #define         MAX_U_32_NUM            0xFFFFFFFF
  162. #ifndef PI
  163. #define         PI                      3.14159265358979
  164. #endif
  165. #define         PI4                     PI/4
  166. #define         PI64                    PI/64
  167. #define         LN_TO_LOG10             0.2302585093
  168.  
  169. #define         VOL_REF_NUM             0
  170. #define         MPEG_AUDIO_ID           1
  171. #define         MAC_WINDOW_SIZE         24
  172.  
  173. #define         MONO                    1
  174. #define         STEREO                  2
  175. #define         BITS_IN_A_BYTE          8
  176. #define         WORD                    16
  177. #define         MAX_NAME_SIZE           81
  178. #define         SBLIMIT                 32
  179. #define         FFT_SIZE                1024
  180. #define         HAN_SIZE                512
  181. #define         SCALE_BLOCK             12
  182. #define         SCALE_RANGE             64
  183. #define         SCALE                   32768
  184. #define         CRC16_POLYNOMIAL        0x8005
  185.  
  186. /* MPEG Header Definitions - Mode Values */
  187.  
  188. #define         MPG_MD_STEREO           0
  189. #define         MPG_MD_JOINT_STEREO     1
  190. #define         MPG_MD_DUAL_CHANNEL     2
  191. #define         MPG_MD_MONO             3
  192.  
  193. /* AIFF Definitions */
  194.  
  195. /* 
  196.  * Note:  The value of a multi-character constant
  197.  *        is implementation-defined.
  198.  */
  199. #if !defined(MS_DOS) && !defined(AIX)  
  200. #define         IFF_LONG
  201. #define         IFF_ID_FORM             'FORM'
  202. #define         IFF_ID_AIFF             'AIFF'
  203. #define         IFF_ID_COMM             'COMM'
  204. #define         IFF_ID_SSND             'SSND'
  205. #define         IFF_ID_MPEG             'MPEG'
  206. #else
  207. #define         IFF_ID_FORM             "FORM"
  208. #define         IFF_ID_AIFF             "AIFF"
  209. #define         IFF_ID_COMM             "COMM"
  210. #define         IFF_ID_SSND             "SSND"
  211. #define         IFF_ID_MPEG             "MPEG"
  212. #endif
  213.  
  214. /* "bit_stream.h" Definitions */
  215.  
  216. #define         MINIMUM         4    /* Minimum size of the buffer in bytes */
  217. #define         MAX_LENGTH      32   /* Maximum length of word written or
  218.                                         read from bit stream */
  219. #define         READ_MODE       0
  220. #define         WRITE_MODE      1
  221. #define         ALIGNING        8
  222. #define         BINARY          0
  223. #define         ASCII           1
  224. #define         BS_FORMAT       BINARY /* BINARY or ASCII = 2x bytes */
  225. #define         BUFFER_SIZE     4096
  226.  
  227. #define         MIN(A, B)       ((A) < (B) ? (A) : (B))
  228. #define         MAX(A, B)       ((A) > (B) ? (A) : (B))
  229.  
  230. /***********************************************************************
  231. *
  232. *  Global Type Definitions
  233. *
  234. ***********************************************************************/
  235.  
  236. /* Structure for Reading Layer II Allocation Tables from File */
  237.  
  238. typedef struct {
  239.     unsigned int    steps;
  240.     unsigned int    bits;
  241.     unsigned int    group;
  242.     unsigned int    quant;
  243. } sb_alloc, *alloc_ptr;
  244.  
  245. typedef sb_alloc        al_table[SBLIMIT][16];
  246.  
  247. /* Header Information Structure */
  248.  
  249. typedef struct {
  250.     int version;
  251.     int lay;
  252.     int error_protection;
  253.     int bitrate_index;
  254.     int sampling_frequency;
  255.     int padding;
  256.     int extension;
  257.     int mode;
  258.     int mode_ext;
  259.     int copyright;
  260.     int original;
  261.     int emphasis;
  262. } layer, *the_layer;
  263.  
  264. /* Parent Structure Interpreting some Frame Parameters in Header */
  265.  
  266. typedef struct {
  267.     layer       *header;        /* raw header information */
  268.     int         actual_mode;    /* when writing IS, may forget if 0 chs */
  269.     al_table    *alloc;         /* bit allocation table read in */
  270.     int         tab_num;        /* number of table as loaded */
  271.     int         stereo;         /* 1 for mono, 2 for stereo */
  272.     int         jsbound;        /* first band of joint stereo coding */
  273.     int         sblimit;        /* total number of sub bands */
  274. } frame_params;
  275.  
  276. /* Double and SANE Floating Point Type Definitions */
  277.  
  278. typedef struct  IEEE_DBL_struct {
  279.     unsigned long   hi;
  280.     unsigned long   lo;
  281. } IEEE_DBL;
  282.  
  283. typedef struct  SANE_EXT_struct {
  284.     unsigned long   l1;
  285.     unsigned long   l2;
  286.     unsigned short  s1;
  287. } SANE_EXT;
  288.  
  289. /* AIFF Type Definitions */
  290.  
  291. typedef char    ID[4];
  292.  
  293. typedef struct  ChunkHeader_struct {
  294.     ID      ckID;
  295.     long    ckSize;
  296. } ChunkHeader;
  297.  
  298. typedef struct  Chunk_struct {
  299.     ID      ckID;
  300.     long    ckSize;
  301.     ID      formType;
  302. } Chunk;
  303.  
  304. typedef struct  CommonChunk_struct {
  305.     ID              ckID;
  306.     long            ckSize;
  307.     short           numChannels;
  308.     unsigned long   numSampleFrames;
  309.     short           sampleSize;
  310.     char            sampleRate[10];
  311. } CommonChunk;
  312.  
  313. typedef struct  SoundDataChunk_struct {
  314.     ID              ckID;
  315.     long            ckSize;
  316.     unsigned long   offset;
  317.     unsigned long   blockSize;
  318. } SoundDataChunk;
  319.  
  320. typedef struct  blockAlign_struct {
  321.     unsigned long   offset;
  322.     unsigned long   blockSize;
  323. } blockAlign;
  324.  
  325. typedef struct  IFF_AIFF_struct {
  326.     short           numChannels;
  327.     unsigned long   numSampleFrames;
  328.     short           sampleSize;
  329.     double          sampleRate;
  330.     unsigned long   sampleType;
  331.     blockAlign      blkAlgn;
  332. } IFF_AIFF;
  333.  
  334. /* "bit_stream.h" Type Definitions */
  335.  
  336. typedef struct  bit_stream_struc {
  337.     FILE        *pt;            /* pointer to bit stream device */
  338.     unsigned char *buf;         /* bit stream buffer */
  339.     int         buf_size;       /* size of buffer (in number of bytes) */
  340.     long        totbit;         /* bit counter of bit stream */
  341.     int         buf_byte_idx;   /* pointer to top byte in buffer */
  342.     int         buf_bit_idx;    /* pointer to top bit of top byte in buffer */
  343.     int         mode;           /* bit stream open in read or write mode */
  344.     int         eob;            /* end of buffer index */
  345.     int         eobs;           /* end of bit stream flag */
  346.     char        format;
  347.     
  348.     /* format of file in rd mode (BINARY/ASCII) */
  349. } Bit_stream_struc;
  350.  
  351. /***********************************************************************
  352. *
  353. *  Global Variable External Declarations
  354. *
  355. ***********************************************************************/
  356.  
  357. extern char     *mode_names[4];
  358. extern char     *layer_names[3];
  359. extern double   s_freq[4];
  360. extern int      bitrate[3][15];
  361. extern double FAR multiple[64];
  362.  
  363. /***********************************************************************
  364. *
  365. *  Global Function Prototype Declarations
  366. *
  367. ***********************************************************************/
  368.  
  369. /* The following functions are in the file "common.c" */
  370.  
  371. #ifdef  PROTO_ARGS
  372. extern FILE           *OpenTableFile(char*);
  373. extern int            read_bit_alloc(int, al_table*);
  374. extern int            pick_table(frame_params*);
  375. extern int            js_bound(int, int);
  376. extern void           hdr_to_frps(frame_params*);
  377. extern void           WriteHdr(frame_params*, FILE*);
  378. extern void           WriteBitAlloc(unsigned int[2][SBLIMIT], frame_params*,
  379.                         FILE*);
  380. extern void           WriteScale(unsigned int[2][SBLIMIT],
  381.                         unsigned int[2][SBLIMIT], unsigned int[2][3][SBLIMIT],
  382.                         frame_params*, FILE*);
  383. extern void           WriteSamples(int, unsigned int FAR [SBLIMIT],
  384.                         unsigned int[SBLIMIT], frame_params*, FILE*);
  385. extern int            NumericQ(char*);
  386. extern int            BitrateIndex(int, int);
  387. extern int            SmpFrqIndex(long);
  388. extern int            memcheck(char*, int, int);
  389. extern void           FAR *mem_alloc(unsigned long, char*);
  390. extern void           mem_free(void**);
  391. extern void           double_to_extended(double*, char[10]);
  392. extern void           extended_to_double(char[10], double*);
  393. extern int            aiff_read_headers(FILE*, IFF_AIFF*);
  394. extern int            aiff_seek_to_sound_data(FILE*);
  395.  
  396.  
  397. /*  Changed by Dan Oberlin -- see function for more info  */
  398. extern int            aiff_write_headers(FILE*, IFF_AIFF*, char *);
  399.  
  400.  
  401. extern int            refill_buffer(Bit_stream_struc*);
  402. extern void           empty_buffer(Bit_stream_struc*, int);
  403. extern void           open_bit_stream_w(Bit_stream_struc*, char*, int);
  404. extern void           open_bit_stream_r(Bit_stream_struc*, char*, int);
  405. extern void           close_bit_stream_r(Bit_stream_struc*);
  406. extern void           close_bit_stream_w(Bit_stream_struc*);
  407. extern void           alloc_buffer(Bit_stream_struc*, int);
  408. extern void           desalloc_buffer(Bit_stream_struc*);
  409. extern void           back_track_buffer(Bit_stream_struc*, int);
  410. extern unsigned int   get1bit(Bit_stream_struc*);
  411. extern void           put1bit(Bit_stream_struc*, int);
  412. extern unsigned long  look_ahead(Bit_stream_struc*, int);
  413. extern unsigned long  getbits(Bit_stream_struc*, int);
  414. extern void           putbits(Bit_stream_struc*, unsigned int, int);
  415. extern void           byte_ali_putbits(Bit_stream_struc*, unsigned int, int);
  416. extern unsigned long  byte_ali_getbits(Bit_stream_struc*, int);
  417. extern unsigned long  sstell(Bit_stream_struc*);
  418. extern int            end_bs(Bit_stream_struc*);
  419. extern int            seek_sync(Bit_stream_struc*, long, int);
  420. extern void           I_CRC_calc(frame_params*, unsigned int[2][SBLIMIT],
  421.                         unsigned int*);
  422. extern void           II_CRC_calc(frame_params*, unsigned int[2][SBLIMIT],
  423.                         unsigned int[2][SBLIMIT], unsigned int*);
  424. extern void           update_CRC(unsigned int, unsigned int, unsigned int*);
  425. extern void           read_absthr(FLOAT*, int);
  426.  
  427. #ifdef  MACINTOSH
  428. extern void           set_mac_file_attr(char[MAX_NAME_SIZE], short, OsType,
  429.                         OsType);
  430. #endif
  431. #ifdef MS_DOS
  432. extern char           *new_ext(char *filename, char *extname); 
  433. #endif
  434.  
  435. #else
  436. extern FILE           *OpenTableFile();
  437. extern int            read_bit_alloc();
  438. extern int            pick_table();
  439. extern int            js_bound();
  440. extern void           hdr_to_frps();
  441. extern void           WriteHdr();
  442. extern void           WriteBitAlloc();
  443. extern void           WriteScale();
  444. extern void           WriteSamples();
  445. extern int            NumericQ();
  446. extern int            BitrateIndex();
  447. extern int            SmpFrqIndex();
  448. extern int            memcheck();
  449. extern void           FAR *mem_alloc();
  450. extern void           mem_free();
  451. extern void           double_to_extended();
  452. extern void           extended_to_double();
  453. extern int            aiff_read_headers();
  454. extern int            aiff_seek_to_sound_data();
  455. extern int            aiff_write_headers();
  456. extern int            refill_buffer();
  457. extern void           empty_buffer();
  458. extern void           open_bit_stream_w();
  459. extern void           open_bit_stream_r();
  460. extern void           close_bit_stream_r();
  461. extern void           close_bit_stream_w();
  462. extern void           alloc_buffer();
  463. extern void           desalloc_buffer();
  464. extern void           back_track_buffer();
  465. extern unsigned int   get1bit();
  466. extern void           put1bit();
  467. extern unsigned long  look_ahead();
  468. extern unsigned long  getbits();
  469. extern void           putbits();
  470. extern void           byte_ali_putbits();
  471. extern unsigned long  byte_ali_getbits();
  472. extern unsigned long  sstell();
  473. extern int            end_bs();
  474. extern int            seek_sync();
  475. extern void           I_CRC_calc();
  476. extern void           II_CRC_calc();
  477. extern void           update_CRC();
  478. extern void           read_absthr();
  479.  
  480. #ifdef MS_DOS
  481. extern char           *new_ext(); 
  482. #endif
  483. #endif
  484.